package admin.employee.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import model.Employee;
import model.Role;
import net.proteanit.sql.DbUtils;

public class EmployeeView extends JPanel {
	private static final long serialVersionUID = 1L;
	private final String NEW = "New";
	private final String UPDATE = "Update";
	private final String DELETE = "Delete";
	private final String SAVE = "Save";
	private final String CANCEL = "Cancel";

	private JTable tableEmployee = null;
	private EmployeeModel employeeModel;

	JPanel buttonPanel;
	JPanel textPanel;
	JPanel tablePanel;

	boolean b = false;// boolean value for enable disable Components function
	boolean isUpdateButton = false;// variable for save button action
	boolean isNewButton = false;// variable for save button action
	private String u = null;// username
	private String p = null;// password

	private JButton newB;
	private JButton updateB;
	private JButton deleteB;
	private JButton saveB;
	private JButton cancelB;

	private JTextField ssnTF;
	private JTextField nameTF;
	private JTextField ageTF;
	private JTextField genderTF;
	private JTextField usernameTF;
	private JTextField passwordTF;
	private JTextField classificationTF;

	private ButtonActionListener buttonActionListener;
	private TableMouseAdapter tableMouseAdapter;

	public EmployeeView() {
		buttonActionListener = new ButtonActionListener();
		tableMouseAdapter = new TableMouseAdapter();
		employeeModel = new EmployeeModel();
		initConponents();
		update();
	}

	public void update() {
		updateView();
	}

	private void updateView() {
		ResultSet rs = null;
		rs = employeeModel.employeeDao.getEmployeeResultSet();
		if (rs != null) {
			tableEmployee.setModel(DbUtils.resultSetToTableModel(rs));
			// tableEmployee.setModel(tableModel);
		} else {
			JOptionPane.showMessageDialog(null, "EmployeeView rs fault");
		}
	}

	private void initConponents() {
		buttonPanel = new JPanel();
		textPanel = new JPanel();
		tablePanel = new JPanel();

		// ***ButtonPanel
		add(buttonPanel);
		newB = new JButton(NEW); // new button
		newB.setActionCommand(NEW);
		newB.addActionListener(buttonActionListener);
		buttonPanel.add(newB);

		updateB = new JButton(UPDATE);
		updateB.setActionCommand(UPDATE);
		updateB.addActionListener(buttonActionListener);
		buttonPanel.add(updateB);

		deleteB = new JButton(DELETE);
		deleteB.setActionCommand(DELETE);
		deleteB.addActionListener(buttonActionListener);
		buttonPanel.add(deleteB);

		saveB = new JButton(SAVE);
		saveB.setActionCommand(SAVE);
		saveB.addActionListener(buttonActionListener);
		saveB.setEnabled(false);
		buttonPanel.add(saveB);

		cancelB = new JButton(CANCEL);
		cancelB.setActionCommand(CANCEL);
		cancelB.addActionListener(buttonActionListener);
		cancelB.setEnabled(false);
		buttonPanel.add(cancelB);

		buttonPanel.setLayout(new GridLayout(5, 1));
		buttonPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Employee Actions"));
		// ***end of ButtonPanel

		// ***TextPanel
		add(textPanel);
		JLabel label = new JLabel("ssn :");
		textPanel.add(label);
		ssnTF = new JTextField();
		textPanel.add(ssnTF);
		label = new JLabel("name :");
		textPanel.add(label);
		nameTF = new JTextField();
		textPanel.add(nameTF);
		label = new JLabel("age :");
		textPanel.add(label);
		ageTF = new JTextField();
		textPanel.add(ageTF);
		label = new JLabel("gender :");
		textPanel.add(label);
		genderTF = new JTextField();
		textPanel.add(genderTF);
		label = new JLabel("username :");
		textPanel.add(label);
		usernameTF = new JTextField();
		textPanel.add(usernameTF);
		label = new JLabel("password :");
		textPanel.add(label);
		passwordTF = new JTextField();
		textPanel.add(passwordTF);
		label = new JLabel("classification :");
		textPanel.add(label);
		classificationTF = new JTextField();
		textPanel.add(classificationTF);
		textPanel.setLayout(new GridLayout(7, 2));
		textPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Employee Detail"));
		// ***end of TextPanel

		// ***TablePanel
		add(tablePanel);
		
		
		//creating a default table model and override its isCellEditable 
		//in order to disabling all the cells from edit from mouse click
		DefaultTableModel model = new DefaultTableModel();
		tableEmployee = new JTable(model) {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		tableEmployee.addMouseListener(tableMouseAdapter);
		JScrollPane scrollPane = new JScrollPane(tableEmployee);
		tablePanel.add(scrollPane);
		// scrollPane.add(tableEmployee);
		tablePanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Employee Table"));
		// ***end of TablePanel
		edComponents(textPanel, false);// set disable textPanel components at
										// the beginning

	}// end initComponents()

	// sets enable or disable all components in a container
	private void edComponents(Container container, boolean b) {
		Component[] components = container.getComponents();
		for (Component component : components) {
			component.setEnabled(b);
			if (component instanceof Container) {
				edComponents((Container) component, b);
			}
		}
	}

	// set correct enable disable button Panel components
	private void edComponents(Container container, String cmd) {
		switch (cmd) {
		case NEW:
			newB.setEnabled(false);
			updateB.setEnabled(false);
			deleteB.setEnabled(false);
			saveB.setEnabled(true);
			cancelB.setEnabled(true);
			break;
		case UPDATE:
			newB.setEnabled(false);
			updateB.setEnabled(false);
			deleteB.setEnabled(false);
			saveB.setEnabled(true);
			cancelB.setEnabled(true);
			break;
		case DELETE:
			// handle delete
			// JOptionPane.showMessageDialog(null, "Delete button pressed");
			break;
		case SAVE:
			newB.setEnabled(true);
			updateB.setEnabled(true);
			deleteB.setEnabled(true);
			saveB.setEnabled(false);
			cancelB.setEnabled(false);
			break;
		case CANCEL:
			newB.setEnabled(true);
			updateB.setEnabled(true);
			deleteB.setEnabled(true);
			saveB.setEnabled(false);
			cancelB.setEnabled(false);
			break;
		}
	}

	class TableMouseAdapter extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			// super.mouseClicked(e);
			if (tableEmployee.isEnabled()) {// only work when tableEmployee is
											// enabled
				System.out.println("mouse clicked on table");
				try {
					int row = tableEmployee.getSelectedRow();
					u = tableEmployee.getModel().getValueAt(row, 4).toString();
					p = tableEmployee.getModel().getValueAt(row, 5).toString();
					setTableRowToTextField(u, p);
				} catch (Exception e2) {
					System.out.println("Mouse listener Exception!");
				}
			}
		}
	}

	class ButtonActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			String cmd = e.getActionCommand();
			switch (cmd) {
			case NEW:
				b = !b;// if its true; set false....& vice versa
				edComponents(textPanel, b);// enable disable components in
											// container
				edComponents(tablePanel, !b);
				edComponents(buttonPanel, cmd);// enable disable components in
												// container
				System.out.println("NEWtbn in employee view");
				isNewButton = !isNewButton;// set isNewButton opposite state
				emptyTextField();
				break;
			case UPDATE:
				if (!ssnTF.getText().isEmpty()) {// only work if there is to
													// update
					b = !b;// if its true; set false....& vice versa
					edComponents(textPanel, b);// enable disable components in
												// container
					ssnTF.setEnabled(false);// shouldnt change ssn number
					edComponents(tablePanel, !b);
					edComponents(buttonPanel, cmd);// enable disable components
													// in container
					System.out.println("UPDATEbtn in employee view ," + b);
					isUpdateButton = !isUpdateButton;// set isUpdateButton
														// opposite state
				} else {
					JOptionPane.showMessageDialog(null,
							"please click first on some item!!");
				}
				break;

			case DELETE:
				if (!ssnTF.getText().isEmpty()) {// only work if there is to
													// delete
					edComponents(buttonPanel, cmd);// enable disable components
													// in container and handle
													// delete button
					System.out.println("DELETEbtn in employee view");
					employeeModel.employeeDao.deleteEmployee(ssnTF.getText());
					updateView();
				} else {
					JOptionPane.showMessageDialog(null,
							"please click first on some item!!");
				}
				break;

			case SAVE:
				b = !b;// if its true; set false....& vice versa
				edComponents(textPanel, b);// enable disable components in
											// container
				edComponents(tablePanel, !b);
				edComponents(buttonPanel, cmd);// enable disable components in
												// container
				// decide action for save button (NewButton or UpdateButton ?)
				if (isUpdateButton) {
					isUpdateButton = !isUpdateButton;
					System.out.println("save for Update Button");
					employeeModel.employeeDao.updateEmployee((ssnTF.getText()),
							(nameTF.getText()), (ageTF.getText()),
							(genderTF.getText()), (usernameTF.getText()),
							(passwordTF.getText()),
							(classificationTF.getText()));
					updateView();
				}
				// decide action for save button (NewButton or UpdateButton ?)
				if (isNewButton) {
					isNewButton = !isNewButton;
					System.out.println("save for New Button");
					employeeModel.employeeDao.newEmployee((ssnTF.getText()),
							(nameTF.getText()), (ageTF.getText()),
							(genderTF.getText()), (usernameTF.getText()),
							(passwordTF.getText()),
							(classificationTF.getText()));
					updateView();
				}
				break;

			case CANCEL:
				isNewButton = !isNewButton;
				isUpdateButton = !isUpdateButton;
				b = !b;// if its true; set false....& vice versa
				edComponents(textPanel, b);// enable disable components in
											// container
				edComponents(tablePanel, !b);
				edComponents(buttonPanel, cmd);// enable disable components in
												// container
				System.out.println("CANCELbtn in employee view");
				if (u != null || p != null) {
					setTableRowToTextField(u, p);
				}
				break;

			default:
				break;
			}
		}
	}// end ButtonActionListener class

	private void setTableRowToTextField(String userName, String password) {
		Employee tmpEmployee = employeeModel.employeeDao.getUser(userName,
				password);
		if (tmpEmployee != null) {
			System.out.println(userName);
			ssnTF.setText(Integer.toString(tmpEmployee.getSsn()));
			nameTF.setText(tmpEmployee.getName());
			ageTF.setText(Integer.toString(tmpEmployee.getAge()));
			genderTF.setText(tmpEmployee.getGender());
			usernameTF.setText(userName);
			passwordTF.setText(password);
			int c;
			if (tmpEmployee.getClassification() == Role.ADMIN)
				c = 2;
			else
				c = 1;
			classificationTF.setText(Integer.toString(c));
		}

	}

	private void emptyTextField() {
		ssnTF.setText("");
		nameTF.setText("");
		ageTF.setText("");
		genderTF.setText("");
		usernameTF.setText("");
		passwordTF.setText("");
		classificationTF.setText("");
	}

}
